<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Pagecontent.php 138 2009-05-25 06:42:38Z movoin $
 */

function _ctlPagecontent($name, $args=null)
{
    $opts = array('pageid', 'return', 'site', 'strlen');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);

    if(!$data['pageid'] || $data['pageid']==0)
        $data['pageid'] = null;
    if(!$data['site'])
        $data['site'] = 0;

    //缓存过期时间
    $deploy = defined('DEPLOY_MODE') && DEPLOY_MODE;
    if ( $deploy ) {
        $lifetime = 86400; //一天
    } else {
        $lifetime = 0;
    }
    //缓存名称
    $cache_id = 'page.' . $name . '.' . $data['site'];
    if($data['pageid'])
        $cache_id .= '.' . $data['pageid'];
    //从缓存中读取数据
    $content = FLEA::getCache($cache_id, $lifetime, true, true);
    if(!is_array($content)) {
        //载入单页模型
        $modelPage =& FLEA::getSingleton( 'Model_Singlepage' );
        $content = $modelPage->getContent($name, $data['site'], $data['pageid']);
        FLEA::writeCache( $cache_id, $content, true );
    }

    if($data['strlen']) {
        if(!function_exists(cutString)) {
            FLEA::loadFile( 'Helper_CutString' );
        }
        return cutString($content[$data['return']], (int)$data['strlen']);
    }

    return $content[$data['return']];

}
